<template>
  <div>
    <el-dialog
      :title="info.isadd?'添加':'修改'"
      :visible.sync="info.isshow"
      @opened="opened"
      @closed="closed"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="上级分类">
          <el-select v-model="form.pid" placeholder="请选择">
            <el-option label="顶级分类" :value="0"></el-option>
            <el-option
              :label="item.catename"
              :value="item.id"
              v-for="item in catelist"
              :key="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分类名称">
          <el-input v-model="form.catename"></el-input>
        </el-form-item>
        <el-form-item label="图片">
          <el-upload
            class="avatar-uploader"
            action="#"
            :show-file-list="false"
            :on-change="onChange"
          >
            <img v-if="form.img" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch v-model="form.status" :active-value="1" :inactive-value="2"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="add">{{info.isadd?'添加':'修改'}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters, mapActions } from "vuex";
import { reqcateadd, reqcatedit } from "../../../request/api.js";
export default {
  data() {
    return {
      gridData: [],
      imageUrl: "",
      form: {
        pid: "",
        catename: "",
        img: "",
        status: ""
      }
    };
  },
  computed: {
    ...mapGetters({
      info: "cate/info",
      cateinfo: "cate/cateinfo",
      catelist: "cate/catelist"
    })
  },
  methods: {
    ...mapActions({
      reqcatelist: "cate/catelist"
    }),
    onChange(e) {
      let file = e.raw;
      this.form.img = file;
      this.imageUrl = URL.createObjectURL(file);
    },
    cancel() {
      this.info.isshow = false;
    },
    add() {
      if (this.info.isadd) {
        reqcateadd(this.form).then(res => {
          if (res.data.code == 200) {
            this.reqcatelist();
            this.info.isshow = false;
          }
        });
      } else {
        reqcatedit(this.form).then(res => {
          if (res.data.code == 200) {
            this.reqcatelist();
            this.info.isshow = false;
          }
        });
      }
      this.form= {
        pid: "",
        catename: "",
        img: "",
        status: ""
      }
    },
    opened() {
      if (!this.info.isadd) {
        this.imageUrl = this.$pre + this.cateinfo.img;
        this.form = this.cateinfo;
      }
    },
    closed() {
      if (!this.info.isadd) {
        this.form = {
          pid: "",
          catename: "",
          img: "",
          status: 1
        };
      }
    }
  }
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>